package com.zk.iotgateway.service.impl;


import com.zk.iotgateway.controller.IotConfigController;
import com.zk.iotgateway.dao.IotJobMapper;
import com.zk.iotgateway.domain.IotJob;
import com.zk.iotgateway.quartz.JobTask;
import com.zk.iotgateway.service.JobService;
import com.zk.iotgateway.util.BeanUtil;
import com.zk.iotgateway.util.JsonUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


@Service
public class JobServiceImpl implements JobService {

    private static final Logger log= LoggerFactory.getLogger(IotConfigController.class);

    @Resource
    IotJobMapper jobMapper;

    @Autowired
    JobTask jobTask;

    @Autowired
    JobService jobService;


    @Override
    public boolean updateJob(IotJob job) {
        try {
            IotJob oldJob = selectByPrimaryKey(job.getId());
            BeanUtil.copyNotNullBean(job, oldJob);
            updateByPrimaryKeySelective(oldJob);
            return true;
        } catch (Exception e) {
            log.error(e.getMessage());
            return false;
        }
    }

    @Override
    public JsonUtil del(String id) {
        JsonUtil j = new JsonUtil();
        j.setFlag(false);
        if (StringUtils.isEmpty(id)) {
            j.setMsg("获取数据失败");
            return j;
        }
        try {
            IotJob job = selectByPrimaryKey(id);
            boolean flag = jobTask.checkJob(job);
            if (flag) {
                j.setMsg("您任务表状态和web任务状态不一致,无法删除");
                return j;
            }
            if (flag) {
                j.setMsg("该任务处于启动中，无法删除");
                return j;
            }
            jobService.deleteByPrimaryKey(id);
            j.setFlag(true);
            j.setMsg("任务删除成功");
        } catch (Exception e) {
            j.setMsg("任务删除异常");
            e.printStackTrace();
        }
        return j;
    }

    @Override
    public boolean startJob(String id) {
        try {
            IotJob job = jobService.selectByPrimaryKey(id);
            jobTask.startJob(job);
            job.setStatus("1");
            jobService.updateByPrimaryKeySelective(job);
            return true;
        } catch (Exception e) {
            log.error(e.getMessage());
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean stopJob(String id) {
        try {
            IotJob job = jobService.selectByPrimaryKey(id);
            jobTask.remove(job);
            job.setStatus("0");
            jobService.updateByPrimaryKeySelective(job);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }



    @Override
    public List<IotJob> selectAll() {
        return jobMapper.selectListByPage(new IotJob());
    }

    @Override
    public IotJob selectByPrimaryKey(String id) {
        return jobMapper.selectByPrimaryKey(id);
    }

    @Override
    public int deleteByPrimaryKey(String id) {
        return jobMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(IotJob record) {
        return jobMapper.insert(record);
    }

    @Override
    public int insertSelective(IotJob record) {
        return jobMapper.insertSelective(record);
    }

    @Override
    public int updateByPrimaryKeySelective(IotJob record) {
        return jobMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(IotJob record) {
        return jobMapper.updateByPrimaryKey(record);
    }

    @Override
    public int deleteByPrimaryKey(IotJob o) {
        return jobMapper.deleteByPrimaryKey(o.getId());
    }

    @Override
    public List<IotJob> selectListByPage(IotJob o) {
        return jobMapper.selectListByPage(o);
    }
}
